// pages/cart/cart.js
const {
  getAddShopping, //加入购物车接口
  getReadingData, // 读取购物车数据接口
  getPurchaseQuantity, //购物车修改购买数量接口
  getChecked, // 购物车修改选中状态接口
  getDelGoods
} = require('../../http/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    par: {
      goodsId: '',
      number: ''
    },
    shoppingList: [],
    totalPrice: 0,
    // 购物车修改购买数量接口
    // purchaseQuantity:{}
    key: '',
    number: '',
    selected: true,
    extent:0,
  },
  async onChange(e) {
    // console.log(e.detail);
    this.setData({
      number: e.detail
    })

    this.data.key = this.data.shoppingList[e.currentTarget.dataset.index].key

    this.data.number = e.detail
    this.setData({
      key: this.data.key,
      number: this.data.number
    })
    // 购物车修改购买数量接口
    const res1 = await getPurchaseQuantity({
      key: this.data.key,
      number: this.data.number,
      token: wx.getStorageSync('token')
    })
    // console.log(res1);
    this.setData({
      shoppingList: res1.data.items
    })
    this.calcTotal()

    // console.log(this.data.number);
  },
  async cut(e) {
    const {
      index
    } = e.currentTarget.dataset
    this.data.key = this.data.shoppingList[index].key
    // this.data.shoppingList[e.currentTarget.dataset.index].selected = e.detail
    // console.log(e);
    // console.log(this.data.shoppingList[e.currentTarget.dataset.index].selected);
    // console.log(this.data.shoppingList);
    this.data.shoppingList[index].selected = !this.data.shoppingList[index].selected
    // console.log(this.data.shoppingList[index].selected);
    this.setData({
      key: this.data.key,
      shoppingList: this.data.shoppingList
      // selected:this.data.shoppingList[e.currentTarget.dataset.index].selected
    })
    this.calcTotal()
    // console.log(111);

    let obj = {
      key: this.data.key,
      selected: this.data.shoppingList[index].selected,
      token: wx.getStorageSync('token')
    }
    // console.log(obj);
    const res2 = await getChecked(obj)
    // console.log(res2);
  },
  async del(e) {
    // console.log(e);
    const index = e.currentTarget.dataset.index
    this.data.key = this.data.shoppingList[index].key
    this.setData({
      key: this.data.key,
    })
    let obj = {
      key: this.data.key,
      token: wx.getStorageSync('token')
    }
      const res3 = await getDelGoods(obj)
    // console.log(this.data.shoppingList[index].number);
    if (this.data.number === 0) {
    console.log(res3);
      this.setData({
        shoppingList: res3.data.items
      })
      return
      // console.log(this.data.shoppingList);
    }

    if(this.data.shoppingList.length===0){
      this.setData({
        shoppingList:[]
      })
    }
    // const res3 = await getDelGoods(obj)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  // 计算总价
  calcTotal() {
    const arr = this.data.shoppingList.filter(item => item.selected)
    if (arr.length === 0) {
      this.data.totalPrice = 0
    } else if (arr.length === 1 ) {
      arr.forEach(item => {
        this.data.totalPrice = item.number * item.price
      })
    } else {
      this.data.totalPrice = arr.reduce((pre, current) => {
        return current.price * current.number + pre
      }, 0)
      console.log(this.data.totalPrice)
      this.setData({
        totalPrice:this.data.totalPrice
      })
    }
    // // 139 * 8 + 159 * 3
    // // 1589
    // // 1589 + 111 * 3
    // this.setData({
    //   totalPrice: this.data.totalPrice
    // })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    const res = await getReadingData(wx.getStorageSync('token'))
    // console.log(res);
    this.setData({
      shoppingList: res.data.items
    })
    this.calcTotal()
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})